<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停车场" prop="parkCode">
        <el-select v-model="queryParams.parkCode" placeholder="请选择停车场" clearable>
          <el-option
            v-for="dict in parkingInfoList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号码" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌颜色" prop="carNumColor">
        <el-select v-model="queryParams.carNumColor" placeholder="请选择车牌颜色" clearable>
          <el-option
            v-for="dict in dict.type.car_num_color"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆类型" prop="carType">
        <el-select v-model="queryParams.carType" placeholder="请选择车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.car_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入场时间">
        <el-date-picker
          v-model="daterangeEntryTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="出场时间">
        <el-date-picker
          v-model="daterangeExitTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="daterangePayTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.pay_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择发票状态" clearable>
          <el-option
            v-for="dict in dict.type.invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          :disabled="true"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['parking:order:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['parking:order:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['parking:order:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parking:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" width="65"/>-->
      <el-table-column label="订单号" align="center" prop="orderNo" width="190"/>
      <el-table-column label="停车场" align="center" prop="parkName" width="120"/>
      <el-table-column label="车牌号码" align="center" prop="carNum" width="100"/>
      <el-table-column label="车牌颜色" align="center" prop="carNumColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_num_color" :value="scope.row.carNumColor"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="carType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.carType"/>
        </template>
      </el-table-column>
      <el-table-column label="入场时间" align="center" prop="entryTime" width="180"/>
      <el-table-column label="出场时间" align="center" prop="exitTime" width="180"/>
      <el-table-column label="停车时长" align="center" prop="totalMinutes" width="100">
        <template slot-scope="scope">
          {{ formatMinutesToHours(scope.row.totalMinutes) }}
        </template>
      </el-table-column>
      <el-table-column label="计费时长" align="center" prop="chargeMinutes" width="100">
        <template slot-scope="scope">
          {{ formatMinutesToHours(scope.row.chargeMinutes) }}
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180"/>
      <el-table-column label="支付金额" align="center" prop="payAmount" />
      <el-table-column label="支付方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_method" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="发票状态" align="center" prop="invoiceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_status" :value="scope.row.invoiceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parking:order:edit']"
          >查看</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['parking:order:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in dict.type.order_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="停车场Code" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入停车场Code" />
        </el-form-item>
        <el-form-item label="停车场名称" prop="parkName">
          <el-input v-model="form.parkName" placeholder="请输入停车场名称" />
        </el-form-item>
        <el-form-item label="停车区域ID" prop="parkAreaId">
          <el-input v-model="form.parkAreaId" placeholder="请输入停车区域ID" />
        </el-form-item>
        <el-form-item label="停车区域名称" prop="parkAreaName">
          <el-input v-model="form.parkAreaName" placeholder="请输入停车区域名称" />
        </el-form-item>
        <el-form-item label="车位ID" prop="parkSpaceId">
          <el-input v-model="form.parkSpaceId" placeholder="请输入车位ID" />
        </el-form-item>
        <el-form-item label="车位编号" prop="parkSpaceNo">
          <el-input v-model="form.parkSpaceNo" placeholder="请输入车位编号" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="carNumColor">
          <el-select v-model="form.carNumColor" placeholder="请选择车牌颜色">
            <el-option
              v-for="dict in dict.type.car_num_color"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-select v-model="form.carType" placeholder="请选择车辆类型">
            <el-option
              v-for="dict in dict.type.car_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入场闸机ID" prop="entryGateId">
          <el-input v-model="form.entryGateId" placeholder="请输入入场闸机ID" />
        </el-form-item>
        <el-form-item label="入场闸机名称" prop="entryGateName">
          <el-input v-model="form.entryGateName" placeholder="请输入入场闸机名称" />
        </el-form-item>
        <el-form-item label="出场闸机ID" prop="exitGateId">
          <el-input v-model="form.exitGateId" placeholder="请输入出场闸机ID" />
        </el-form-item>
        <el-form-item label="出场闸机名称" prop="exitGateName">
          <el-input v-model="form.exitGateName" placeholder="请输入出场闸机名称" />
        </el-form-item>
        <el-form-item label="识别摄像头ID" prop="cameraId">
          <el-input v-model="form.cameraId" placeholder="请输入识别摄像头ID" />
        </el-form-item>
        <el-form-item label="入场时间" prop="entryTime">
          <el-date-picker clearable
            v-model="form.entryTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择入场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出场时间" prop="exitTime">
          <el-date-picker clearable
            v-model="form.exitTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择出场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计出场时间" prop="expectedExitTime">
          <el-date-picker clearable
            v-model="form.expectedExitTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预计出场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总停车分钟数" prop="totalMinutes">
          <el-input v-model="form.totalMinutes" placeholder="请输入总停车分钟数" />
        </el-form-item>
        <el-form-item label="免费分钟数" prop="freeMinutes">
          <el-input v-model="form.freeMinutes" placeholder="请输入免费分钟数" />
        </el-form-item>
        <el-form-item label="计费分钟数" prop="chargeMinutes">
          <el-input v-model="form.chargeMinutes" placeholder="请输入计费分钟数" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="completeTime">
          <el-date-picker clearable
            v-model="form.completeTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取消时间" prop="cancelTime">
          <el-date-picker clearable
            v-model="form.cancelTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择取消时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="actualAmount">
          <el-input v-model="form.actualAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-select v-model="form.payMethod" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.pay_method"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付渠道交易号" prop="transNo">
          <el-input v-model="form.transNo" placeholder="请输入支付渠道交易号" />
        </el-form-item>
        <el-form-item label="发票状态" prop="invoiceStatus">
          <el-select v-model="form.invoiceStatus" placeholder="请选择发票状态">
            <el-option
              v-for="dict in dict.type.invoice_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票ID" prop="invoiceId">
          <el-input v-model="form.invoiceId" placeholder="请输入发票ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/parking/order";
import { selectParkingInfoList } from "@/api/parking/selectList";

export default {
  name: "Order",
  dicts: ['invoice_status', 'order_status', 'car_num_color', 'pay_method', 'order_type', 'car_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车订单表格数据
      orderList: [],
      parkingInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除标志时间范围
      daterangeEntryTime: [],
      // 删除标志时间范围
      daterangeExitTime: [],
      // 删除标志时间范围
      daterangePayTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        orderStatus: null,
        orderType: null,
        parkCode: null,
        parkName: null,
        parkAreaId: null,
        parkAreaName: null,
        parkSpaceId: null,
        parkSpaceNo: null,
        carNum: null,
        carNumColor: null,
        carType: null,
        entryGateId: null,
        entryGateName: null,
        exitGateId: null,
        exitGateName: null,
        cameraId: null,
        entryTime: null,
        exitTime: null,
        expectedExitTime: null,
        totalMinutes: null,
        freeMinutes: null,
        chargeMinutes: null,
        payTime: null,
        completeTime: null,
        cancelTime: null,
        totalAmount: null,
        discountAmount: null,
        actualAmount: null,
        payAmount: null,
        payMethod: null,
        transNo: null,
        invoiceStatus: null,
        invoiceId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        entryTime: [
          { required: true, message: "入场时间不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "总金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParkingInfoList();
  },
  methods: {
    /** 查询停车订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEntryTime && '' != this.daterangeEntryTime) {
        this.queryParams.params["beginEntryTime"] = this.daterangeEntryTime[0];
        this.queryParams.params["endEntryTime"] = this.daterangeEntryTime[1];
      }
      if (null != this.daterangeExitTime && '' != this.daterangeExitTime) {
        this.queryParams.params["beginExitTime"] = this.daterangeExitTime[0];
        this.queryParams.params["endExitTime"] = this.daterangeExitTime[1];
      }
      if (null != this.daterangePayTime && '' != this.daterangePayTime) {
        this.queryParams.params["beginPayTime"] = this.daterangePayTime[0];
        this.queryParams.params["endPayTime"] = this.daterangePayTime[1];
      }
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkingInfoList() {
      selectParkingInfoList().then(response => {
        this.parkingInfoList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        orderStatus: null,
        orderType: null,
        parkCode: null,
        parkName: null,
        parkAreaId: null,
        parkAreaName: null,
        parkSpaceId: null,
        parkSpaceNo: null,
        carNum: null,
        carNumColor: null,
        carType: null,
        entryGateId: null,
        entryGateName: null,
        exitGateId: null,
        exitGateName: null,
        cameraId: null,
        entryTime: null,
        exitTime: null,
        expectedExitTime: null,
        totalMinutes: null,
        freeMinutes: null,
        chargeMinutes: null,
        payTime: null,
        completeTime: null,
        cancelTime: null,
        totalAmount: null,
        discountAmount: null,
        actualAmount: null,
        payAmount: null,
        payMethod: null,
        transNo: null,
        invoiceStatus: null,
        invoiceId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.queryParams.payTime){
        this.queryParams.payTime = this.queryParams.payTime + ' 00:00:00';
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEntryTime = [];
      this.daterangeExitTime = [];
      this.daterangePayTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停车订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除停车订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    formatMinutesToHours(minutes) {
      if (isNaN(minutes) || minutes < 0) {
        return "0分";
      }

      const hours = Math.floor(minutes / 60);
      const remainingMinutes = minutes % 60;

      if (hours === 0) {
        return `${remainingMinutes}分`;
      } else if (remainingMinutes === 0) {
        return `${hours}小时`;
      } else {
        return `${hours}小时${remainingMinutes}分`;
      }
    },

  }
};
</script>
